$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/purmh/addPurmh.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$('#updateForm').submit(function() {
		$.ajax({
			url: '/wms/purmh/updatePurmh.xhtml',
			data: $('#updateForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("更新成功!");
					closeDialogUpdate();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	optFormater();
	ajaxTable();
	setDialogAdd();
	closeDialogAdd();
	setDialogUpdate();
	closeDialogUpdate();
});
function ajaxTable() {
	$('#purmhTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			iconCls : 'icon-add',
			handler : function() {
				if(globalMaps.get('3') != undefined&&(mgac=='N'||mgac=='S')) {
					openDialogAdd();
				}
			}
		}, '-', {
			id : 'toolbarSearch',
			iconCls : 'icon-remove',
			handler : function() {
				if(globalMaps.get('3') != undefined&&(mgac=='N'||mgac=='S')) {
					delPurmh();
				}
			}
		}
		],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
		},
		onClickRow : function(rowIndex, rowData) {
			$('#purmhTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			if(globalMaps.get('3') != undefined&&(mgac=='N'||mgac=='S')) {
				updatePurmhSet(rowData.mhac,rowData.mhad,rowData.mhae,rowData.mhaf,rowData.mhag,rowData.mhah,rowData.mhai,rowData.mhaj,rowData.mhak,rowData.mhal,rowData.mham);
			}
	    }
	}).datagrid('acceptChanges');
}
function reloadTable() {
	$('#purmhTable').datagrid('reload');
	$('#purmhTable').datagrid('clearSelections');
}
function optFormater() {
	var update = '';
	if(globalMaps.get('4') != undefined && mgac == 'N') {
		update += "<a href='#' onclick='deletePurmgmh(\""+mgaa+"\",\""+mgab+"\")' class='easyui-linkbutton' plain='true' iconCls='icon-cancel'>删除</a>";
	}
	if(globalMaps.get('5') != undefined && mgac == 'N') {
		update += "<a href='#' onclick='auditPurmgmh(\""+mgaa+"\",\""+mgab+"\",\"Y\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>审核</a>";
	}
	if(globalMaps.get('6') != undefined && mgac == 'Y') {
		update += "<a href='#' onclick='auditPurmgmh(\""+mgaa+"\",\""+mgab+"\",\"C\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>指定结束</a>";
		update += "<a href='#' onclick='auditPurmgmh(\""+mgaa+"\",\""+mgab+"\",\"N\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>撤销审核</a>";
	}
	if(globalMaps.get('6') != undefined && mgac == 'C') {
		update += "<a href='#' onclick='auditPurmgmh(\""+mgaa+"\",\""+mgab+"\",\"Y\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>撤销结束</a>";
	}
	if(globalMaps.get('7') != undefined && mgac == 'Y') {
		update += "<a href='#' onclick='auditPurmgmh(\""+mgaa+"\",\""+mgab+"\",\"V\")' class='easyui-linkbutton' plain='true' iconCls='icon-tip'>作废</a>";
	}
	if(globalMaps.get('3') != undefined && mgac == 'S') {
		update += "<a href='#' onclick='auditPurmgmh(\""+mgaa+"\",\""+mgab+"\",\"N\")' class='easyui-linkbutton' plain='true' iconCls='icon-ok'>保存</a>";
	}
	$("#menuDiv").html(update);
	$.parser.parse($("#menuDiv"));
}
//----------------新增Begin
function setDialogAdd() {
	$('#purmhAdd').dialog({
		title : '新增单身',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogAdd() {
//	addPurmhReset();
	$('#purmhAdd').dialog('open');
}
function closeDialogAdd() {
	addPurmhReset();
	$('#purmhAdd').dialog('close');
}
function purmhAdd() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
function addPurmhReset() {
	$("#purmhAddMhad").val("");
	$("#purmhAddMhae").val("");
	$("#purmhAddMhaf").val("");
	$("#purmhAddMhag").val("");
	$("#purmhAddMhah").val("");
	$("#purmhAddMhai").val("");
	$("#purmhAddMhaj").val("");
	$("#purmhAddMhak").val("");
	$("#purmhAddMhal").val("");
	$("#purmhAddMham").val("");
}
//----------------新增End
//----------------更新Begin
function setDialogUpdate() {
	$('#purmhUpdate').dialog({
		title : '编辑',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogUpdate() {
	$('#purmhUpdate').dialog('open');
}
function closeDialogUpdate() {
	updatePurmhReset();
	$('#purmhUpdate').dialog('close');
}
function purmhUpdate() {
	var validateResult = $("#updateForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#updateForm').submit();
}
function updatePurmhReset() {
	$("#purmhUpdateMhad").val("");
	$("#purmhUpdateMhae").val("");
	$("#purmhUpdateMhaf").val("");
	$("#purmhUpdateMhag").val("");
	$("#purmhUpdateMhah").val("");
	$("#purmhUpdateMhai").val("");
	$("#purmhUpdateMhaj").val("");
	$("#purmhUpdateMhak").val("");
	$("#purmhUpdateMhal").val("");
	$("#purmhUpdateMham").val("");
}
function updatePurmhSet(mhac,mhad,mhae,mhaf,mhag,mhah,mhai,mhaj,mhak,mhal,mham) {
	openDialogUpdate();
	$("#purmhUpdateMhac").val(mhac);
	$("#purmhUpdateMhad").val(mhad);
	$("#purmhUpdateMhae").val(mhae);
	$("#purmhUpdateMhaf").val(mhaf);
	$("#purmhUpdateMhag").val(mhag);
	$("#purmhUpdateMhah").val(mhah);
	$("#purmhUpdateMhai").val(mhai);
	$("#purmhUpdateMhaj").val(mhaj);
	$("#purmhUpdateMhak").val(mhak);
	$("#purmhUpdateMhal").val(mhal);
	$("#purmhUpdateMham").val(mham);
}
//----------------更新End
//----------------删除Begin
function delPurmh(){
	if ($('#purmhTable').datagrid('getSelected')) {
		var ids = [];
		var selectedRow = $('#purmhTable').datagrid('getSelections');
		for ( var i = 0; i < selectedRow.length; i++) {
			ids.push(selectedRow[i].mhac);
		}
		var mhac = ids.join(',');
		changeStatus('/wms/purmh/delPurmh.xhtml?mhac=' + mhac+'&mhaa='+mgaa+'&mhab='+mgab);
		$('#purmhTable').datagrid('clearSelections');
	}
}
function delPurmhAll(){
	changeStatus('/wms/purmh/delPurmhAll.xhtml?mhaa='+mgaa+'&mhab='+mgab);
}
//----------------删除End
//-------------SHENHE---
function auditPurmgmh(mgaa,mgab,flag) {
	changeStatus('/wms/purmh/auditPurmh.xhtml?mgaa=' + mgaa + '&mgab=' + mgab+ '&flag=' + flag);
	window.parent.returnValue=1;
	window.close();

}
function deletePurmgmh(mgaa,mgab) {
	$.messager.confirm('删除提示', '你确定删除该采购单吗?', function(r) {
		if(r) {
			changeStatus('/wms/purmh/delPurmgmh.xhtml?mgaa=' + mgaa + '&mgab=' + mgab);
			window.parent.returnValue=1;
			window.close();
		}
	});
}
function changeStatus(url){
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		dataType : "text",
		url : url,
		error : function () {
			alert('请求失败');
		},
		success : function(result){
			if(result=="success") {
//				alert("操作成功!");
			} else {
				alert(result);
			}
			reloadTable();
		}
	});
}
window.onbeforeunload = function(){  
	var n = window.event.screenX - window.screenLeft;   
    var b = n > document.documentElement.scrollWidth-20;   //		
      if(b&&window.event.clientY<0||window.event.altKey){   
    	  delPurmhAll();
//          alert("是关闭而非刷新");   
//          window.event.returnValue   =   "是否关闭？";
      }else{
//             alert("是刷新而非关闭");   
     }  
 } ; 

